package cn.edu.zzti.cs.controller.admin;


import cn.edu.zzti.cs.service.student.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class LoginController {

    private StudentService studentService;
    @Autowired
    public void setStudentService(StudentService studentService) {
        this.studentService = studentService;
    }

    @RequestMapping(value = "submit",method = RequestMethod.POST,produces =
            "application/json;charset=utf-8")
    public String loginSubmit(@RequestParam("userNum")String userNum,
                              @RequestParam("password")String password,
                              HttpServletResponse response){
        if (studentService.loginAuthenticate(userNum,password)){
// 认证成功，重定向到主页面
            return "redirect:/html/index.html";
        } else {
// 认证失败
            response.setContentType("text/html;charset=utf-8");
            try {
                response.getWriter().write("<script>alert('账号或密码错误');history.back()</script>");
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                return null;
            }
        }
    }
}
